package com.cgsrp.cgsrp_user_province.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.cgsrp.cgsrp_shopping.dao.UserWalletMapper;
import com.cgsrp.cgsrp_shopping.domain.UserWallet;
import com.cgsrp.cgsrp_user_province.dao.UserMapper;
import com.cgsrp.cgsrp_user_province.domain.User;
import com.cgsrp.cgsrp_user_province.service.UserService;
import com.cgsrp.utils.JwtUtil;
import com.cgsrp.utils.Result;
import com.cgsrp.vo.LoginVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserWalletMapper walletMapper;


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public LoginVo login(User user) {
        LoginVo loginVo=new LoginVo();
        User user0;
        user0=userMapper.selectByEmail(user.getUsername1());
        if (user0==null){
            user0=userMapper.selectByPhone(user.getUsername1());
            if (user0==null){
                try {
                    user0=userMapper.selectByUsername(Long.parseLong(user.getUsername1()));
                } catch (NumberFormatException e) {
                    loginVo.setResult(Result.failure("系统故障"));
                    return loginVo;
                }
                if (user0==null){
                    loginVo.setResult(Result.failure("用户不存在，请检查用户名/手机号/邮箱"));
                    return loginVo;
                }
            }
        }
        if (user0.getIsLock()==1){
            loginVo.setResult(Result.failure("此用户账号已经被封禁"));
            return loginVo;
        }
        if (user0.getPassword().equals(DigestUtil.sha256Hex(user.getPassword()))){
            String token = JwtUtil.createToken(user0.getUsername().toString());
            loginVo.setUser(user0);
            loginVo.setResult(Result.ok(token));
            return loginVo;
        }else {
            loginVo.setResult(Result.failure("登录失败，密码错误"));
            return loginVo;
        }
    }


    @Override
    public boolean selectByPhone(String phone) {
        return userMapper.selectByPhone(phone)==null;
    }

    @Override
    public boolean selectByEmail(String email) {
        return userMapper.selectByEmail(email)==null;
    }

    @Override
    public Object regist(User user) {
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        class Vo{
            private boolean flag;
            private String msg;
        }
        Vo vo = new Vo();
        // 使用密码加密
        // sha256算法是非对称加密，是不可以逆的
        // 加密网站：http://www.ttmd5.com/hash.php?type=9
        user.setPassword(DigestUtil.sha256Hex(user.getPassword()));
        int i=userMapper.addUser(user);
        if (i==1){
            vo.setFlag(true);
            vo.setMsg("注册成功");
            return vo;
        }else {
            vo.setFlag(false);
            vo.setMsg("注册失败");
            return vo;
        }
    }

    @Override
    public Long findMaxUsername() {
        return userMapper.findMaxUsername();
    }

    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }

    @Override
    public Result getWallet(Integer userId) {
        UserWallet userWallet = walletMapper.selectByUserId(userId);
        if (userWallet==null){
            UserWallet userWallet1 = new UserWallet();
            userWallet1.setPoint(0);
            userWallet1.setBalance(new BigDecimal("0.00"));
            return Result.suc("成功",userWallet1);
        }else {
            return Result.suc("成功",userWallet);
        }
    }

}
